package com.deercrowing.takeaway.controller.admin;

import com.deercrowing.takeaway.common.model.Result;
import com.deercrowing.takeaway.vo.SetmealsPageVO;
import com.deercrowing.takeaway.vo.SetmealsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import jakarta.validation.constraints.NotNull;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import com.deercrowing.takeaway.entity.SetmealsEntity;
import com.deercrowing.takeaway.service.SetmealsService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐表(Setmeal)表控制层
 *
 * @author guoyongxiang
 * @since 2024-07-11 14:34:50
 */
@Slf4j
@Validated
@RestController
@RequestMapping("admin/setmeals")
public class SetmealsController {
    /**
     * 服务对象
     */
    @Autowired
    private SetmealsService setmealsService;

    /**
     * 分页查询所有数据
     *
     * @param setmealsPageVO 查询实体
     * @return 所有数据
     */
    @GetMapping("/list")
    public ResponseEntity selectAll(SetmealsPageVO setmealsPageVO) {
        return ResponseEntity.ok(Result.OK(this.setmealsService.serachByCondition(setmealsPageVO)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public ResponseEntity selectOne(@PathVariable @NotNull(message = "主键不能为空") Long id) {
        if (ObjectUtils.isEmpty(this.setmealsService.getById(id)))
        {
            throw new NullPointerException();
        }
        return ResponseEntity.ok(Result.OK(this.setmealsService.selectById(id)));
    }

    /**
     * 新增数据
     *
     * @param setmealsVO 实体对象
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity insert(@RequestBody SetmealsVO setmealsVO) {
        if (ObjectUtils.isEmpty(setmealsVO)){
            throw new NullPointerException();
        }

        this.setmealsService.insertInfoToDB(setmealsVO);
        return ResponseEntity.ok(Result.OK());
    }

    /**
     * 修改数据
     *
     * @param id 主键, setmealEntity 实体对象
     * @return 修改结果
     */
    @PutMapping("/{id}")
    public ResponseEntity update(@PathVariable @NotNull(message = "主键不能为空") Long id, @RequestBody SetmealsVO setmealsVO) {

        this.setmealsService.updateInfoToDB(id,setmealsVO);
        return ResponseEntity.ok(Result.OK());
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public ResponseEntity delete(@RequestParam("ids") List<Long> idList) {
        this.setmealsService.deleteById(idList);
        return ResponseEntity.ok(Result.OK());
    }

    /**
     * 修改套餐状态
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/status")
    public ResponseEntity statusUpdate(@RequestParam @NotNull(message = "主键不能为空") Integer id,
                                       @RequestParam @NotNull(message = "状态不能为空") Short status){

        this.setmealsService.setmealStatusUpdate(status,id);
        return ResponseEntity.ok(Result.OK());
    }
}

